package com.liufujie.baidumapdemo.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.liufujie.baidumapdemo.R;
import com.liufujie.baidumapdemo.mapapi.overlayutil.DrivingRouteOverlay;
import com.liufujie.baidumapdemo.mapapi.overlayutil.TransitRouteOverlay;
import com.liufujie.baidumapdemo.mapapi.overlayutil.WalkingRouteOverlay;

public class PlanActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnWalk, btnDrive, btnTransit;

    private EditText etStart, etEnd;

    private MapView mMapView;

    private BaiduMap mBaiduMap;

    private RoutePlanSearch mSearch;

    private OnGetRoutePlanResultListener listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SDKInitializer.initialize(getApplication());

        setContentView(R.layout.activity_plan);

        init();

        setCenter();
    }

    /**
     * 初始化控件
     */
    private void init() {
        btnWalk = (Button) findViewById(R.id.btn_walk);
        btnDrive = (Button) findViewById(R.id.btn_drive);
        btnTransit = (Button) findViewById(R.id.btn_transit);

        btnWalk.setOnClickListener(this);
        btnDrive.setOnClickListener(this);
        btnTransit.setOnClickListener(this);

        etStart = (EditText) findViewById(R.id.et_start);
        etEnd = (EditText) findViewById(R.id.et_end);

        mMapView = (MapView) findViewById(R.id.mv);

        mBaiduMap = mMapView.getMap();

        mSearch = RoutePlanSearch.newInstance();

        listener = new OnGetRoutePlanResultListener() {
            @Override
            public void onGetWalkingRouteResult(WalkingRouteResult result) {
                if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                    Toast.makeText(PlanActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
                }
                if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
                    //起终点或途经点地址有岐义，通过以下接口获取建议查询信息
                    //result.getSuggestAddrInfo()
                    return;
                }
                if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                    WalkingRouteOverlay walkingRouteOverlay = new WalkingRouteOverlay(mBaiduMap);
                    if (result.getRouteLines().size() > 0) {
                        walkingRouteOverlay.setData(result.getRouteLines().get(0));

                        mBaiduMap.setOnMarkerClickListener(walkingRouteOverlay);
                        walkingRouteOverlay.addToMap();
                        walkingRouteOverlay.zoomToSpan();
                    }
                }
            }

            @Override
            public void onGetTransitRouteResult(TransitRouteResult result) {
                if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                    Toast.makeText(PlanActivity.this, "抱歉，" +
                            "未找到结果", Toast.LENGTH_SHORT).show();
                }
                if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
                    //起终点或途经点地址有岐义，通过以下接口获取建议查询信息
                    //result.getSuggestAddrInfo()
                    return;
                }
                if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                    //检索成功，将路径规划覆盖物添加到地图中
                    TransitRouteOverlay transitRouteOverlay = new TransitRouteOverlay(mBaiduMap);
                    //给覆盖物设置数据源：数据源就是检索结果
                    if (result.getRouteLines().size() > 0) {
                        transitRouteOverlay.setData(result.getRouteLines().get(0));
                        //给百度地图设置路线规划覆盖物的点击事件监听器，使用百度地图提供的默认响应方法
                        mBaiduMap.setOnMarkerClickListener(transitRouteOverlay);
                        transitRouteOverlay.addToMap();//将覆盖物添加到地图中
                        transitRouteOverlay.zoomToSpan();//自动缩放地图
                    }
                }
            }

            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult result) {
                if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                    Toast.makeText(PlanActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
                }
                if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
                    //起终点或途经点地址有岐义，通过以下接口获取建议查询信息
                    //result.getSuggestAddrInfo()
                    return;
                }
                if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                    DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(mBaiduMap);
                    if (result.getRouteLines().size() > 0) {
                        drivingRouteOverlay.setData(result.getRouteLines().get(0));

                        mBaiduMap.setOnMarkerClickListener(drivingRouteOverlay);
                        drivingRouteOverlay.addToMap();
                        drivingRouteOverlay.zoomToSpan();
                    }
                }
            }
        };

        mSearch.setOnGetRoutePlanResultListener(listener);
    }

    /**
     * 公交路线规划
     */
    private void clickTransitRoute() {
        if (!"".equals(etStart.getText().toString()) && !"".equals(etEnd.getText().toString())) {

            PlanNode startNode = PlanNode.withCityNameAndPlaceName("广州", etStart.getText().toString());
            PlanNode endNode = PlanNode.withCityNameAndPlaceName("广州", etEnd.getText().toString());

            TransitRoutePlanOption option = new TransitRoutePlanOption()
                    .from(startNode)
                    .city("广州")
                    .to(endNode);

            mSearch.transitSearch(option);
        }
    }

    /**
     * 驾车路线规划
     */
    private void clickDriveRoute() {
        if (!"".equals(etStart.getText().toString()) && !"".equals(etEnd.getText().toString())) {
            PlanNode startNode = PlanNode.withCityNameAndPlaceName("广州", etStart.getText().toString());
            PlanNode endNode = PlanNode.withCityNameAndPlaceName("广州", etEnd.getText().toString());

            mSearch.drivingSearch(new DrivingRoutePlanOption()
                    .from(startNode)
                    .to(endNode));
        }
    }

    private void clickWalkRoute() {
        if (!"".equals(etStart.getText().toString()) && !"".equals(etEnd.getText().toString())) {
            PlanNode startNode = PlanNode.withCityNameAndPlaceName("广州", etStart.getText().toString());
            PlanNode endNode = PlanNode.withCityNameAndPlaceName("广州", etEnd.getText().toString());

            mSearch.walkingSearch(new WalkingRoutePlanOption()
                    .from(startNode)
                    .to(endNode));
        }
    }

    // 设置中心点,和缩放等级
    private void setCenter() {
        LatLng position = new LatLng(23.090035, 113.302216);
        MapStatusUpdate center = MapStatusUpdateFactory.newLatLng(position);
        mBaiduMap.setMapStatus(center);

        // 设置缩放等级
        MapStatusUpdate zoom = MapStatusUpdateFactory.zoomTo(15);
        mBaiduMap.setMapStatus(zoom);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_walk:
                mBaiduMap.clear();
                clickWalkRoute();
                break;
            case R.id.btn_drive:
                mBaiduMap.clear();
                clickDriveRoute();
                break;
            case R.id.btn_transit:
                mBaiduMap.clear();
                clickTransitRoute();
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
        mSearch.destroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }
}
